﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.ComponentModel.DataAnnotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
using EMS.Calendar.Services;

namespace EMS.Calendar.Models {
    public class CalendarPart : ContentPart<CalendarPartRecord> {
        public EventOccurrenceType EventOccurrence {
            get {
                EventOccurrenceType tempEventOccurrenceType;
                if (Enum.TryParse<EventOccurrenceType>(Record.EventOccurrence, out tempEventOccurrenceType))
                    return tempEventOccurrenceType;
                else
                    return EventOccurrenceType.Single;
            }
            set { Record.EventOccurrence = value.ToString(); }
        }
        [Required]
        public DateTime? StartDate {
            get { return Record.StartDate; }
            set { Record.StartDate = value; }
        }
        public DateTime? StartTime {
            get { return Record.StartTime; }
            set { Record.StartTime = value; }
        }
        public DateTime? EndDate {
            get { return Record.EndDate; }
            set { Record.EndDate = value; }
        }
        public DateTime? EndTime {
            get { return Record.EndTime; }
            set { Record.EndTime = value; }
        }
        public bool IsAllDayEvent {
            get { return Record.IsAllDayEvent; }
            set { Record.IsAllDayEvent = value; }
        }
        public PatternType Pattern {
            get {
                PatternType tempPatternType;
                if (Enum.TryParse<PatternType>(Record.Pattern, out tempPatternType))
                    return tempPatternType;
                else
                    return PatternType.Daily;
            }
            set { Record.Pattern = value.ToString(); }
        }
        public int PeriodSpan {
            get { return Record.PeriodSpan; }
            set { Record.PeriodSpan = value; }
        }
        public int DayOfPeriod {
            get { return Record.DayOfPeriod; }
            set { Record.DayOfPeriod = value; }
        }
        public DayInstanceType DayInstance {
            get {
                DayInstanceType tempDayInstanceType;
                if (Enum.TryParse<DayInstanceType>(Record.DayInstance, out tempDayInstanceType))
                    return tempDayInstanceType;
                else
                    return DayInstanceType.first;
            }
            set { Record.DayInstance = value.ToString(); }
        }
        public List<DayNameType> DayNames {
            get { return StaticCalendarServices.GetDayNameListFromCSV(Record.DayNames); }
            set { Record.DayNames = StaticCalendarServices.GetDayNameCSVFromList(value); }
        }
        public MonthNameType MonthName {
            get {
                MonthNameType tempMonthNameType;
                if (Enum.TryParse<MonthNameType>(Record.MonthName, out tempMonthNameType))
                    return tempMonthNameType;
                else
                    return MonthNameType.January;
            }
            set { Record.MonthName = value.ToString(); }
        }
        public RecurrenceEndType RecurrenceEnd {
            get {
                RecurrenceEndType tempRecurrenceEndType;
                if (Enum.TryParse<RecurrenceEndType>(Record.RecurrenceEnd, out tempRecurrenceEndType))
                    return tempRecurrenceEndType;
                else
                    return RecurrenceEndType.None;
            }
            set { Record.RecurrenceEnd = value.ToString(); }
        }
        public int NumberOccurrences {
            get { return Record.NumberOccurrences; }
            set { Record.NumberOccurrences = value; }
        }
    }
}